package org.zjh.controller.timer;

import org.zjh.model.Poker;
import org.zjh.view.game.GamePanel;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @description: 发牌移动定时器
 * @author 郑佳豪
 * @date 2024/10/11 21:53
 * @version 1.0
 */
public class MoveObjTimer {
    private Poker poker;
    private int targetX;
    private int targetY;
    private GamePanel gamePanel;
    private Timer timer;
    private ActionListener actionListener = new ActionListener() {
        //当前步数
        int currentStep = 0;

        @Override
        public void actionPerformed(ActionEvent e) {
            // 计算每一步扑克的新位置
            int startX = poker.getX();
            int startY = poker.getY();
            int steps = 10;
            int newX = startX + (targetX - startX) * currentStep / steps;
            int newY = startY + (targetY - startY) * currentStep / steps;

            // 更新扑克的位置
            poker.setLocation(newX, newY);

            // 重新绘制面板
            gamePanel.repaint();

            // 增加当前步数
            currentStep++;

            // 如果动画结束，停止定时器
            if (currentStep > steps) {
                timer.stop();
            }
        }
    };


    public MoveObjTimer(Poker poker, int targetX, int targetY, GamePanel gamePanel){
        this.poker = poker;
        this.targetX = targetX;
        this.targetY = targetY;
        this.gamePanel = gamePanel;
        timer = new Timer(8,actionListener);
        timer.start();
    }


}
